package com.example.lenovo.appstore.utils;

import android.content.Intent;
import android.content.res.Configuration;

import com.example.lenovo.appstore.MainActivity;

import java.util.Locale;

/**
 * 项目名称：AppStore
 *
 * @Author 郭迎辉
 * Create by 16:12 on 2019/3/22.
 * Describe:
 */
public class Language {
    public static void shiftLanguage() {
        String sta = AppContext.getInstance().getResources().getConfiguration().locale.getLanguage();
        //shiftLanguage(sta);
        if (sta.equals("zh")) {
            SaveAndGetData.saveString(AppContext.getInstance(), SaveAndGetData.isEnglish, "en");
            Locale.setDefault(Locale.ENGLISH);
            Configuration config = AppContext.getInstance().getResources().getConfiguration();
            config.locale = Locale.ENGLISH;
            AppContext.getInstance().getResources().updateConfiguration(config, AppContext.getInstance().getResources().getDisplayMetrics());
            refreshSelf();
        } else {
            SaveAndGetData.saveString(AppContext.getInstance(), SaveAndGetData.isEnglish, "zh");
            Locale.setDefault(Locale.CHINESE);
            Configuration config =AppContext.getInstance(). getResources().getConfiguration();
            config.locale = Locale.CHINESE;
            AppContext.getInstance().getResources().updateConfiguration(config, AppContext.getInstance().getResources().getDisplayMetrics());
            refreshSelf();
        }
    }

    public static  void refreshSelf() {
        ActivityCollector.finishAll();
        Intent intent = new Intent(AppContext.getInstance(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        AppContext.getInstance().startActivity(intent);
    }
}
